package dao.impl;

import dao.BaseDao;
import dao.HouseDao;
import pojo.House;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author :kuexun
 * @description : todo
 * @create : 2019/9/2 21:38
 */
public class HouseDaoImpl extends BaseDao implements HouseDao{
    @Override
    public List<House> findHouses() {
        String sql = "select * from house";
        List<House> list = new ArrayList<>();
        try {
            Connection connection = getCon();
            PreparedStatement psm = connection.prepareStatement(sql);

            ResultSet rs = psm.executeQuery();
            while(rs.next()) {
                House house = new House();
                house.setId(rs.getInt("id"));
                house.setUserId(rs.getInt("user_id"));
                house.setTypeId(rs.getInt("type_id"));
                house.setTitle(rs.getString("title"));
                house.setDescription(rs.getString("description"));
                house.setContact(rs.getString("contact"));
                house.setFloorAge(rs.getInt("floorage"));
                house.setStreetId(rs.getInt("street_id"));
                house.setPrice(rs.getDouble("price"));
//                house.setPubDate(rs.getString("pubdate"));

                list.add(house);
            }
        } catch(SQLException e) {
            e.printStackTrace();
        }
        return list.size()>0?list:null;
    }

    @Override
    public boolean addHouse(House house) {

        String sql = "insert into house(user_id,type_id,title,description,price,pubdate,FLOORAGE,CONTACT,STREET_ID) " +
                "values (?,?,?,?,?,now(),?,?,?)";
        try {
            Connection connection = getCon();
            PreparedStatement psm = connection.prepareStatement(sql);
            psm.setInt(1,house.getUserId());
            psm.setInt(2,house.getTypeId());
            psm.setString(3,house.getTitle());
            psm.setString(4,house.getDescription());
            psm.setDouble(5,house.getPrice());
            psm.setInt(6,house.getFloorAge());
            psm.setString(7,house.getContact());
            psm.setInt(8,house.getStreetId());
            int i = psm.executeUpdate();
            if(i > 0) {
                return true;
            }

        } catch(SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean updateHouse(House house) {

        String sql = "update house set user_id = ?,type_id = ?,title = ?,description = ?,price = ?,pubdate = now()," +
                "FLOORAGE = ?,CONTACT = ?,STREET_ID = ? where ID = ?";
        try {
            Connection connection = getCon();
            PreparedStatement psm = connection.prepareStatement(sql);
            psm.setInt(1,house.getUserId());
            psm.setInt(2,house.getTypeId());
            psm.setString(3,house.getTitle());
            psm.setString(4,house.getDescription());
            psm.setDouble(5,house.getPrice());
            psm.setInt(6,house.getFloorAge());
            psm.setString(7,house.getContact());
            psm.setInt(8,house.getStreetId());
            psm.setInt(9,house.getId());
            int i = psm.executeUpdate();
            if(i > 0) {
                return true;
            }

        } catch(SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean delHouse(int id) {
        String sql = "delete from house where id = ?";

        try {
            Connection connection = getCon();
            PreparedStatement psm = connection.prepareStatement(sql);
            psm.setInt(1,id);
            int i = psm.executeUpdate();
            if(i > 0){
                return true;
            }
        } catch(SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
